
#include "device/dev_common.h"
#include "serial/serial.h"
#include "usb/camera.h"
#include "usb/disk.h"
#include "usb/keyboard.h"
#include "usb/net.h"
#include "usb/sdcard.h"


namespace iecas_ns
{
    shared_ptr<CDevCommon> CDevCommon::sm_intancePtr = nullptr;

    CDevCommon::CDevCommon()
    {
        m_commonPtr = shared_ptr<CCommon>(CCommon::getInstance());
    }

	shared_ptr<CDevCommon> CDevCommon::getInstance()
	{
		if(sm_intancePtr == nullptr)
		{
			sm_intancePtr = shared_ptr<CDevCommon>(new CDevCommon());
		}
		return sm_intancePtr;
	}

    void CDevCommon::init_serial(string srcDev)
    {
        thread tempThread(&CDevCommon::new_serial, this, srcDev);
        tempThread.detach();
    }

    void CDevCommon::new_serial(string srcDev)
    {
        shared_ptr<CSerial> serial_Ptr;
        serial_Ptr = shared_ptr<CSerial>(new CSerial());
        sleep(1);
        if(serial_Ptr->init(srcDev))
        {
            serial_Ptr->loop();
        }
    }

    void CDevCommon::deinit_serial(string srcDev)
    {
        shared_ptr<CSerial> serial_Ptr;
        serial_Ptr = CSerial::getInstance();
        serial_Ptr->deinit(srcDev);
    }

    void CDevCommon::init_usb_disk(string srcDev)
    {
        thread tempThread(&CDevCommon::new_usb_disk, this, srcDev);
        tempThread.detach();
    }

    void CDevCommon::new_usb_disk(string srcDev)
    {
        shared_ptr<CDisk> disk_Ptr;
        disk_Ptr = shared_ptr<CDisk>(new CDisk());
        sleep(1);
        if(disk_Ptr->init(srcDev))
        {
            disk_Ptr->loop();
        }
    }

    void CDevCommon::deinit_usb_disk(string srcDev)
    {
        shared_ptr<CDisk> disk_Ptr;
        disk_Ptr = CDisk::getInstance();
        disk_Ptr->deinit(srcDev);
    }

    void CDevCommon::init_usb_keyboard(string srcDev)
    {
        thread tempThread(&CDevCommon::new_usb_keyboard, this, srcDev);
        tempThread.detach(); 
    }

    void CDevCommon::new_usb_keyboard(string srcDev)
    {
        shared_ptr<CKeyboard> keyboard_Ptr;
        keyboard_Ptr = shared_ptr<CKeyboard>(new CKeyboard());
        sleep(1);
        if(keyboard_Ptr->init(srcDev))
        {
            keyboard_Ptr->loop();
        }
    }

    void CDevCommon::deinit_usb_keyboard(string srcDev)
    {
        shared_ptr<CKeyboard> keyboard_Ptr;
        keyboard_Ptr = CKeyboard::getInstance();
        keyboard_Ptr->deinit(srcDev);
    }

    void CDevCommon::init_usb_camera(string srcDev)
    {
        thread tempThread(&CDevCommon::new_usb_camera, this, srcDev);
        tempThread.detach();
    }

    void CDevCommon::new_usb_camera(string srcDev)
    {
        shared_ptr<CCamera> camera_Ptr;
        camera_Ptr = shared_ptr<CCamera>(new CCamera());
        sleep(1);
        if(camera_Ptr->init(srcDev))
        {
            camera_Ptr->loop();
        }
    }

    void CDevCommon::deinit_usb_camera(string srcDev)
    {
        shared_ptr<CCamera> camera_Ptr;
        camera_Ptr = CCamera::getInstance();
        camera_Ptr->deinit(srcDev);
    }

    void CDevCommon::init_usb_net(string srcDev)
    {
        thread tempThread(&CDevCommon::new_usb_net, this, srcDev);
        tempThread.detach();
    }

    void CDevCommon::new_usb_net(string srcDev)
    {
        shared_ptr<CNet> net_Ptr;
        net_Ptr = shared_ptr<CNet>(new CNet());
        sleep(1);
        if(net_Ptr->init(srcDev))
        {
            net_Ptr->loop();
        }
    }

    void CDevCommon::deinit_usb_net(string srcDev)
    {
        shared_ptr<CNet> net_Ptr;
        net_Ptr = CNet::getInstance();
        net_Ptr->deinit(srcDev);
    }

    void CDevCommon::init_sdcard(string srcDev)
    {
        thread tempThread(&CDevCommon::new_sdcard, this, srcDev);
        tempThread.detach();
    }

    void CDevCommon::new_sdcard(string srcDev)
    {
        shared_ptr<CSdcard> sdcard_Ptr;
        sdcard_Ptr = shared_ptr<CSdcard>(new CSdcard());
        sleep(1);
        if(sdcard_Ptr->init(srcDev))
        {
            sdcard_Ptr->loop();
        }
    }

    void CDevCommon::deinit_sdcard(string srcDev)
    {
        shared_ptr<CSdcard> sdcard_Ptr;
        sdcard_Ptr = CSdcard::getInstance();
        sdcard_Ptr->deinit(srcDev);
    }
}




